/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MailboxPanel.java
 *
 * Created on Mar 30, 2011, 8:08:59 PM
 */

package org.libarae.client;

import java.net.SocketTimeoutException;
import java.util.Calendar;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import org.libarae.communication.ClientInterface;
import org.libarae.communication.common.MessageThread;
import org.libarae.communication.request.MailboxMessagesRequest;
import org.libarae.communication.response.MailboxMessagesResponse;
import org.libarae.communication.response.Response;

/**
 *
 * @author Casey, Ben
 */
@SuppressWarnings("serial")
public class MailboxPanel extends javax.swing.JPanel {

    private Vector<MessageThread> mMessageThreads;

    /** Creates new form MailboxPanel */
    public MailboxPanel() {

        initComponents();
    }

    public void refreshMailbox()
    {
        MailboxMessagesRequest r = new MailboxMessagesRequest();

        Response response = null;

        try {
            response = ClientInterface.sendRequest(r);
        }
        catch(SocketTimeoutException e)
        {
            JOptionPane.showMessageDialog(null, "Request timed out. Please try again.");
            return;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        if(response == null)
        {
            System.err.println("Could not connect to server");
        }

        if(response.getStatus() == Response.STATUS_GOOD)
        {
            MailboxMessagesResponse mmr = (MailboxMessagesResponse)response;

            mMessageThreads = mmr.getMessageThreads();

            DefaultListModel model = new DefaultListModel();

            if(mMessageThreads.size() == 0)
            {
                model.add(0, "<html><br>No threads found.<br><br></html>");
            }
            else
            {
                for(MessageThread mt : mMessageThreads)
                {
                    Calendar time = Calendar.getInstance();
                    time.setTimeInMillis(mt.getTime());

                    model.add(model.getSize(), "<html><br>Sent at " + String.format("%tT", time) + "<br>From: " +
                            mt.getUserName1() + "<br>" + "Subject: " +  mt.getSubject() + "<br><br></html>");
                }
            }

            lstThreads.setModel(model);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrThreads = new javax.swing.JScrollPane();
        lstThreads = new javax.swing.JList();

        lstThreads.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lstThreads.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Quinn\tMessage\t\tAdmin\t\t\tUnread" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstThreads.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                lstThreadsMousePressed(evt);
            }
        });
        lstThreads.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstThreadsValueChanged(evt);
            }
        });
        scrThreads.setViewportView(lstThreads);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scrThreads, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scrThreads, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void lstThreadsValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstThreadsValueChanged
        
    }//GEN-LAST:event_lstThreadsValueChanged

    private void lstThreadsMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstThreadsMousePressed
        if(evt.getClickCount() > 1 && mMessageThreads.size() > 0)
        {
            MessageThread mt = mMessageThreads.get(lstThreads.getSelectedIndex());
            MessageDialog md = new MessageDialog(mt);

            md.setVisible(true);
        }
    }//GEN-LAST:event_lstThreadsMousePressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList lstThreads;
    private javax.swing.JScrollPane scrThreads;
    // End of variables declaration//GEN-END:variables

}
